import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/purchase_quality_problem_odoo_api.dart';
import 'package:flutter_admin/models/odoo/purchase_quality_problem.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/odoo/product_productunitprice/product_list.dart';
import 'package:flutter_admin/pages/odoo/purchase_quality_problem/sipplier_list.dart';
import 'package:flutter_admin/pages/odoo/udt_tb_docmentstock/dms_file/dms_file_list.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';

import '../../../generated/l10n.dart';

// 质量检测记录，编辑详情记录的弹窗
class PurchaseQualityProblemEdit extends StatefulWidget {
  PurchaseQualityProblemEdit(
      {this.purchaseQualityProblem,
      required this.isHandle,
      this.isEditable = true});

  final PurchaseQualityProblem? purchaseQualityProblem;

  final bool isHandle;

  final bool isEditable;

  @override
  _PurchaseQualityProblemEditState createState() =>
      _PurchaseQualityProblemEditState();
}

class _PurchaseQualityProblemEditState
    extends State<PurchaseQualityProblemEdit> {
  late PurchaseQualityProblem purchaseQualityProblem;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.purchaseQualityProblem == null;
    this.purchaseQualityProblem =
        widget.purchaseQualityProblem ?? PurchaseQualityProblem();
  }

  _selectProduct() async {
    showDialog(
      context: context,
      builder: (BuildContext context) => Dialog(
        child: ProductList(),
      ),
    ).then((v) {
      if (v != null) {
        //_query();
        formKey.currentState!.save();
        purchaseQualityProblem.productName =
            v['name'] != false ? v['name'] : '';
        purchaseQualityProblem.spec =
            v['description'] != false ? v['description'] : '';
        setState(() {});
      }
    });
  }

  _selectSipplier() async {
    showDialog(
      context: context,
      builder: (BuildContext context) => Dialog(
        child: SipplierList(),
      ),
    ).then((v) {
      if (v != null) {
        //_query();
        formKey.currentState!.save();
        purchaseQualityProblem.supplierName =
            v['name'] != false ? v['name'] : '';
        setState(() {});
      }
    });
  }

  List<Widget> _formList() {
    // if (widget.isEditable) {
    if (widget.isHandle) {
      List<Widget> list = [
        Row(
          children: [
            Expanded(
              child: CryInput(
                label: '流水号',
                value: purchaseQualityProblem.serialnumber,
                enable: false,
                onSaved: (v) {
                  purchaseQualityProblem.serialnumber = v;
                },
              ),
            ),
            Expanded(
              child: CryInput(
                label: '产品',
                enable: false,
                value: purchaseQualityProblem.productName,
                onSaved: (v) {
                  purchaseQualityProblem.productName = v;
                },
              ),
            ),
          ],
        ),
        Row(
          children: [
            Expanded(
              child: CryInput(
                label: '供应单位',
                enable: false,
                value: purchaseQualityProblem.supplierName,
                onSaved: (v) {
                  purchaseQualityProblem.supplierName = v;
                },
              ),
            ),
            Expanded(
              child: CryInput(
                label: '规格型号',
                enable: false,
                value: purchaseQualityProblem.spec,
                onSaved: (v) {
                  purchaseQualityProblem.spec = v;
                },
              ),
            ),
          ],
        ),
        Row(
          children: [
            Expanded(
              child: CryInput(
                // label: '来料日期',
                label: '检测日期',
                enable: false,
                value: purchaseQualityProblem.jcrq,
                onSaved: (v) {
                  purchaseQualityProblem.jcrq = v;
                },
              ),
            ),
            Expanded(
              child: CryInputNum(
                label: '抽检数量',
                enable: false,
                value: purchaseQualityProblem.checkqty,
                onSaved: (v) {
                  purchaseQualityProblem.checkqty = v;
                },
              ),
            ),
          ],
        ),
        Row(
          children: [
            Expanded(
              child: CryInput(
                label: '来料数量',
                enable: false,
                value: purchaseQualityProblem.instockqty,
                onSaved: (v) {
                  purchaseQualityProblem.instockqty = v;
                },
              ),
            ),
            Expanded(
              child: CryInputNum(
                label: '不合格数量',
                enable: false,
                value: purchaseQualityProblem.unqualifiedqty,
                onSaved: (v) {
                  purchaseQualityProblem.unqualifiedqty = v;
                },
              ),
            ),
          ],
        ),
        CryInput(
          label: '问题描述',
          enable: false,
          value: purchaseQualityProblem.questiondescription,
          onSaved: (v) {
            purchaseQualityProblem.questiondescription = v;
          },
        ),
        CryInput(
          label: '处理方式',
          enable: false,
          value: purchaseQualityProblem.zlbpsyj,
          onSaved: (v) {
            purchaseQualityProblem.zlbpsyj = v;
          },
        ),
        CryInput(
          label: '采购人',
          enable: false,
          value: purchaseQualityProblem.cgb,
          onSaved: (v) {
            purchaseQualityProblem.cgb = v;
          },
        ),
        CryInput(
          label: '检验人',
          enable: false,
          value: purchaseQualityProblem.jianyanren,
          onSaved: (v) {
            purchaseQualityProblem.jianyanren = v;
          },
        ),
        CryInput(
          label: '文档号',
          enable: false,
          value: purchaseQualityProblem.bhx,
          onSaved: (v) {
            purchaseQualityProblem.bhx = v;
          },
        ),
        CryInput(
          label: '原因分析',
          value: purchaseQualityProblem.causeanalysis,
          enable: widget.isEditable,
          onSaved: (v) {
            purchaseQualityProblem.causeanalysis = v;
          },
        ),
        CryInput(
          label: '改进措施',
          enable: widget.isEditable,
          value: purchaseQualityProblem.rectificationmeasures,
          onSaved: (v) {
            purchaseQualityProblem.rectificationmeasures = v;
          },
        ),
        CryInput(
          enable: widget.isEditable,
          label: '跟踪情况验证',
          value: purchaseQualityProblem.effectiveverification,
          onSaved: (v) {
            purchaseQualityProblem.effectiveverification = v;
          },
        ),
      ];
      return list;
    }
    List<Widget> list = [
      Row(
        children: [
          Expanded(
            child: CryInput(
              label: '流水号',
              value: purchaseQualityProblem.serialnumber,
              enable: widget.isEditable,
              onSaved: (v) {
                purchaseQualityProblem.serialnumber = v;
              },
              validator: (v) {
                return v!.isEmpty ? S.of(context).required : null;
              },
            ),
          ),
          Expanded(
            child: CryInput(
              label: '产品',
              value: purchaseQualityProblem.productName,
              enable: widget.isEditable,
              onSaved: (v) {
                purchaseQualityProblem.productName = v;
              },
              onTap: () => {_selectProduct()},
              validator: (v) {
                return v!.isEmpty ? "请选择产品" : null;
              },
            ),
          ),
        ],
      ),
      Row(
        children: [
          Expanded(
            child: CryInput(
              label: '供应单位',
              value: purchaseQualityProblem.supplierName,
              enable: widget.isEditable,
              onSaved: (v) {
                purchaseQualityProblem.supplierName = v;
              },
              onTap: () => {_selectSipplier()},
              validator: (v) {
                return v!.isEmpty ? "请选择供应单位" : null;
              },
            ),
          ),
          Expanded(
            child: CryInput(
              label: '规格型号',
              enable: widget.isEditable,
              value: purchaseQualityProblem.spec,
              onSaved: (v) {
                purchaseQualityProblem.spec = v;
              },
            ),
          ),
        ],
      ),
      Row(
        children: [
          Expanded(
            child: widget.isEditable
                ? CrySelectDate(
                    context,
                    label: '检测日期',
                    // label: '来料日期',
                    value: purchaseQualityProblem.jcrq,
                    onSaved: (v) {
                      purchaseQualityProblem.jcrq = v;
                    },
                    validator: (v) {
                      return v!.isEmpty ? S.of(context).required : null;
                    },
                  )
                : CryInput(
                    label: '检测日期',
                    value: purchaseQualityProblem.supplierName,
                    enable: widget.isEditable,
                    onSaved: (v) {
                      purchaseQualityProblem.supplierName = v;
                    },
                    onTap: () => {_selectSipplier()},
                    validator: (v) {
                      return v!.isEmpty ? "请选择供应单位" : null;
                    },
                  ),
          ),
          Expanded(
            child: CryInputNum(
              enable: widget.isEditable,
              label: '抽检数量',
              value: purchaseQualityProblem.checkqty,
              onSaved: (v) {
                purchaseQualityProblem.checkqty = v;
              },
            ),
          ),
        ],
      ),
      Row(
        children: [
          Expanded(
            child: CryInput(
              enable: widget.isEditable,
              label: '来料数量',
              value: purchaseQualityProblem.instockqty,
              onSaved: (v) {
                purchaseQualityProblem.instockqty = v;
              },
            ),
          ),
          Expanded(
            child: CryInputNum(
              enable: widget.isEditable,
              label: '不合格数量',
              value: purchaseQualityProblem.unqualifiedqty,
              onSaved: (v) {
                purchaseQualityProblem.unqualifiedqty = v;
              },
            ),
          ),
        ],
      ),
      CryInput(
        label: '问题描述',
        enable: widget.isEditable,
        value: purchaseQualityProblem.questiondescription,
        onSaved: (v) {
          purchaseQualityProblem.questiondescription = v;
        },
      ),
      CryInput(
        label: '检验人',
        enable: widget.isEditable,
        value: purchaseQualityProblem.jianyanren,
        onSaved: (v) {
          purchaseQualityProblem.jianyanren = v;
        },
      ),
      CryInput(
        label: '处理方式',
        enable: widget.isEditable,
        value: purchaseQualityProblem.zlbpsyj,
        onSaved: (v) {
          purchaseQualityProblem.zlbpsyj = v;
        },
      ),
      CryInput(
        label: '处理人',
        enable: widget.isEditable,
        value: purchaseQualityProblem.handleuserName,
        onSaved: (v) {
          purchaseQualityProblem.handleuserName = v;
        },
      ),
      CryInput(
        label: '采购人',
        enable: widget.isEditable,
        value: purchaseQualityProblem.cgb,
        onSaved: (v) {
          purchaseQualityProblem.cgb = v;
        },
      ),
      CryInput(
        enable: widget.isEditable,
        label: '文档号',
        value: 'KD04-JC-001',
        onSaved: (v) {
          purchaseQualityProblem.bhx = v;
        },
      ),
    ];
    return list;
    // } else {
    //   return detailInfoList();
    // }
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(children: _formList()),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        if (widget.isEditable)
          CryButton(
            label: S.of(context).save,
            onPressed: () {
              FormState form = formKey.currentState!;
              if (!form.validate()) {
                return;
              }
              form.save();
              if (widget.isHandle) {
                UserInfo userInfo = StoreUtil.getCurrentUserInfo();
                purchaseQualityProblem.status = '1';
                purchaseQualityProblem.handleuserName = userInfo.userName;
                print(purchaseQualityProblem.handleuserName);
                purchaseQualityProblem.handledatetime =
                    DateFormat('yyyy-MM-dd hh:mm:ss').format(DateTime.now());
              } else {
                purchaseQualityProblem.status = PurchaseQualityProblem.CREATED;
              }
              purchaseQualityProblem.isPush = '0';
              PurchaseQualityProblemOdooApi.saveOrUpdate(
                      purchaseQualityProblem.toOdooMap())
                  .then((ResponseBodyApi res) {
                if (!res.success!) {
                  //package:flutter_admin/pages/common/cry(re s.message!);
                  return;
                }
                if (res.data["code"] != 200) {
                  CryUtils.message(res.data["message"]);
                  return;
                }
                Navigator.pop(context, true);
                CryUtils.message(S.of(context).saved);
              });
            },
            iconData: Icons.save,
          ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd!
            ? S.of(context).add
            : widget.isEditable
                ? S.of(context).modify
                : S.of(context).show_problem_detail),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }

  List<Widget> detailInfoList() {
    List<Widget> list = [
      Row(
        children: [
          Expanded(
            child: CryInput(
              label: '流水号',
              value: purchaseQualityProblem.serialnumber,
              enable: false,
              onSaved: (v) {
                purchaseQualityProblem.serialnumber = v;
              },
            ),
          ),
          Expanded(
            child: CryInput(
              label: '产品',
              enable: false,
              value: purchaseQualityProblem.productName,
              onSaved: (v) {
                purchaseQualityProblem.productName = v;
              },
            ),
          ),
        ],
      ),
      Row(
        children: [
          Expanded(
            child: CryInput(
              label: '供应单位',
              enable: false,
              value: purchaseQualityProblem.supplierName,
              onSaved: (v) {
                purchaseQualityProblem.supplierName = v;
              },
            ),
          ),
          Expanded(
            child: CryInput(
              label: '规格型号',
              enable: false,
              value: purchaseQualityProblem.spec,
              onSaved: (v) {
                purchaseQualityProblem.spec = v;
              },
            ),
          ),
        ],
      ),
      Row(
        children: [
          Expanded(
            child: CryInput(
              // label: '来料日期',
              label: '检测日期',
              enable: false,
              value: purchaseQualityProblem.jcrq,
              onSaved: (v) {
                purchaseQualityProblem.jcrq = v;
              },
            ),
          ),
          Expanded(
            child: CryInputNum(
              label: '抽检数量',
              enable: false,
              value: purchaseQualityProblem.checkqty,
              onSaved: (v) {
                purchaseQualityProblem.checkqty = v;
              },
            ),
          ),
        ],
      ),
      Row(
        children: [
          Expanded(
            child: CryInput(
              label: '来料数量',
              enable: false,
              value: purchaseQualityProblem.instockqty,
              onSaved: (v) {
                purchaseQualityProblem.instockqty = v;
              },
            ),
          ),
          Expanded(
            child: CryInputNum(
              label: '不合格数量',
              enable: false,
              value: purchaseQualityProblem.unqualifiedqty,
              onSaved: (v) {
                purchaseQualityProblem.unqualifiedqty = v;
              },
            ),
          ),
        ],
      ),
      CryInput(
        label: '问题描述',
        enable: false,
        value: purchaseQualityProblem.questiondescription,
        onSaved: (v) {
          purchaseQualityProblem.questiondescription = v;
        },
      ),
      CryInput(
        label: '处理方式',
        enable: false,
        value: purchaseQualityProblem.zlbpsyj,
        onSaved: (v) {
          purchaseQualityProblem.zlbpsyj = v;
        },
      ),
      CryInput(
        label: '采购人',
        enable: false,
        value: purchaseQualityProblem.cgb,
        onSaved: (v) {
          purchaseQualityProblem.cgb = v;
        },
      ),
      CryInput(
        label: '检验人',
        enable: false,
        value: purchaseQualityProblem.jianyanren,
        onSaved: (v) {
          purchaseQualityProblem.jianyanren = v;
        },
      ),
      CryInput(
        label: '文档号',
        enable: false,
        value: purchaseQualityProblem.bhx,
        onSaved: (v) {
          purchaseQualityProblem.bhx = v;
        },
      ),
      CryInput(
        label: '原因分析',
        value: purchaseQualityProblem.causeanalysis,
        onSaved: (v) {
          purchaseQualityProblem.causeanalysis = v;
        },
      ),
      CryInput(
        label: '改进措施',
        value: purchaseQualityProblem.rectificationmeasures,
        onSaved: (v) {
          purchaseQualityProblem.rectificationmeasures = v;
        },
      ),
      CryInput(
        label: '跟踪情况验证',
        value: purchaseQualityProblem.effectiveverification,
        onSaved: (v) {
          purchaseQualityProblem.effectiveverification = v;
        },
      ),
    ];
    return list;
  }
}
